package com.labichaoka.chaoka.base;

import android.app.Activity;
import android.content.Intent;
import android.webkit.JavascriptInterface;

import com.just.agentweb.AgentWeb;
import com.labichaoka.chaoka.ui.MainActivity;
import com.labichaoka.chaoka.ui.account.LoginActivity;
import com.labichaoka.chaoka.ui.record.BorrowRecordActivity;
import com.labichaoka.chaoka.utils.ShareUtils;
import com.labichaoka.chaoka.utils.ToastUtils;


/**
 * Created by ruan on 2018/9/7.
 */

public class JsBridgeInterface {

    private AgentWeb agent;
    private Activity act;
    private ShareUtils shareUtils;

    public JsBridgeInterface(AgentWeb agent, Activity activity) {
        this.agent = agent;
        this.act = activity;
        shareUtils = new ShareUtils();
    }

    @JavascriptInterface
    public void superShare(String shareType, String title, String description, String webUrl, String imgUrl) {
        if (shareUtils.isWeixinAvilible(MyApplication.mContext)) {
            shareUtils.share(shareType, title, description, webUrl, imgUrl, act);
        } else {
            ToastUtils.show("请先安装微信!");
        }

    }

    @JavascriptInterface
    public void closeWebView() {
        act.finish();
    }

    @JavascriptInterface
    public boolean isNative() {
        return true;
    }

    @JavascriptInterface
    public void openNativeActivity(String className) {
        Intent intent = new Intent();
        switch (className) {
            case "borrow":
                MyApplication.finishAllActivityExceptMain(MainActivity.class);
                intent.setClass(act, BorrowRecordActivity.class);
                break;
            case "main":
                MyApplication.finishAllActivityExceptMain(MainActivity.class);
                break;
            case "login":
                MyApplication.finishAllActivityExceptMain(MainActivity.class);
                intent.setClass(act, LoginActivity.class);

                break;

        }
        act.startActivity(intent);
    }


}
